<template>
    <div id="login">
        <el-form label-width="100px">
            <el-form-item label="用户名">
                <el-input v-model="user.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input type="password" v-model="user.password"></el-input>
            </el-form-item>
            <el-form-item label="验证码">
                <el-input v-model.number="user.code" class="code"></el-input>
                <img :src="img" alt="">
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login">提交</el-button>
                <el-button type="primary" @click="getStudents">获取学生信息</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data(){
        return {
            user:{
                username:'',
                password:'',
                code:'',
                uuid:''
            },
            img:''
        }
    },
    mounted(){
        this.getCode()
    },
    methods:{
        // 获取验证码
        async getCode(){
            const {data:res} = await this.$http.get('/captchaImage')
            if(res.status === 200){
                this.img = res.data.img
                this.user.uuid = res.data.uuid
            }
        },
        // 登录
        async login(){
            const {data:res} = await this.$http.post('/login',this.user)
            localStorage.setItem('token',res.data.token)
            console.log(res)
        },
        async getStudents(){
            const {data:res} = await this.$http.get('/students',{
                params:{
                    pagenum:1,
                    pagesize:3
                }
            })
            console.log(res)
        }
    }
}
</script>

<style lang="less" scoped>
    #login{
        width: 500px;
    }
    .code{
        width: 100px;
    }
</style>